package com.learn.dp;

/**
 * 求解两个字符串最长公共子序列
 */
public class LargestCommonSeqStr {
    public int process(char[] str, char[] str2) {
        int[][] dp = new int[str.length + 1][str2.length + 1];
        dp[0][0] = str[0] == str[0] ? 1 : 0;

        for (int i = 1; i < str.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], str[i] == str2[0] ? 1 : 0);
        }

        for (int i = 1; i < str2.length; i++) {
            dp[0][i] = Math.max(dp[0][i - 1], str[0] == str2[i] ? 1 : 0);
        }

        for (int i = 1; i < str.length; i++) {
            for (int j = 1; j < str.length; j++) {
                dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                if (str[i] == str2[j]) {
                    Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
                }
            }
        }


        return dp[str.length - 1][str2.length - 1];

    }
}
